package com.gl.goods.widght;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gl.goods.R;


/**
 * 自定义LoadingLayout
 */
public class LoadingLayout extends LinearLayout implements View.OnClickListener {

    public static final int LOADING = 0;
    public static final int NO_DATA = 1;
    public static final int NO_NETWORK = 2;
    public static final int GONE = 3;

    private ImageView mIvLoading;
    private TextView mTvLoading;
    private ImageView mIvError;
    private TextView mTvError;
    private RelativeLayout mRlError;
    private LinearLayout mLlLoading;
    private Button mBtnActionNegative, mBtnActionPositive;
    private Animation mAnimLoading;
    private View mView;

    private int mErrorDrawable;
    private String mErrorText;
    private String mNegativeText;
    private String mPositiveText;

    private OnActionNegativeListener mNegativeListener;
    private OnActionPositiveListener mPositiveListener;

    public LoadingLayout(Context context) {
        this(context, null, 0);
    }

    public LoadingLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadingLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.layout_loading, this);
        mIvLoading = (ImageView) mView.findViewById(R.id.iv_loading);
        mTvLoading = (TextView) mView.findViewById(R.id.tv_loading);
        mIvError = (ImageView) mView.findViewById(R.id.iv_error);
        mLlLoading = (LinearLayout) mView.findViewById(R.id.ll_loading);
        mRlError = (RelativeLayout) mView.findViewById(R.id.rl_error);
        mBtnActionNegative = (Button) mView.findViewById(R.id.btn_action_negative);
        mBtnActionPositive = (Button) mView.findViewById(R.id.btn_action_positive);
        mTvError = (TextView) mView.findViewById(R.id.tv_error);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LoadingLayout, defStyleAttr, 0);

        mAnimLoading = AnimationUtils.loadAnimation(context, R.anim.anim_footer_loading);

        mErrorDrawable = a.getResourceId(R.styleable.LoadingLayout_error_drawable, R.mipmap.no_data);
        mErrorText = a.getString(R.styleable.LoadingLayout_error_text);
        mNegativeText = a.getString(R.styleable.LoadingLayout_negative_text);
        mPositiveText = a.getString(R.styleable.LoadingLayout_positive_text);

        a.recycle();

        if (mErrorText != null){
            mTvError.setText(mErrorText);
        }

        if (mErrorDrawable != 0) {
            mIvError.setImageResource(mErrorDrawable);
        }

        if (mNegativeText != null){
            mBtnActionNegative.setText(mNegativeText);
        }
        if (mPositiveText != null){
            mBtnActionPositive.setText(mPositiveText);
        }

        mRlError.setOnClickListener(this);
        mBtnActionNegative.setOnClickListener(this);
        mBtnActionPositive.setOnClickListener(this);

        setStatue(GONE);
    }

    public void setStatue(int status) {
        if (status == LOADING) {//加载
            mRlError.setVisibility(View.GONE);
            mLlLoading.setVisibility(View.VISIBLE);
            mIvLoading.setVisibility(VISIBLE);
            mIvLoading.startAnimation(mAnimLoading);
            setVisibility(View.VISIBLE);
        } else if (status == NO_DATA) {//无数据情况
            mIvLoading.setAnimation(null);
            mIvLoading.setVisibility(View.GONE);
            mRlError.setVisibility(View.VISIBLE);
            mLlLoading.setVisibility(View.GONE);
            setVisibility(View.VISIBLE);
        } else if (status == NO_NETWORK) {//无网络情况
            mIvLoading.setAnimation(null);
            mIvLoading.setVisibility(View.GONE);
            mRlError.setVisibility(View.VISIBLE);
            mLlLoading.setVisibility(View.GONE);
            setVisibility(View.VISIBLE);
        } else {  //隐藏Loading
            mIvLoading.setAnimation(null);
            mIvLoading.setVisibility(View.GONE);
            setVisibility(View.GONE);
        }
    }

    /**
     * 设置加载显示文本
     * @param text
     */
    public void setLoadingText(String text) {
        if (text != null) {
            mTvLoading.setText(text);
        }
    }

    /**
     * 设置错误信息
     * @param text
     * @param resId
     */
    public void setErrorInfo(String text, int resId) {
        if (text != null && resId != 0) {
            mTvError.setText(text);
            mIvError.setImageResource(resId);
            if (text.startsWith("网络请求失败")) {
                mBtnActionNegative.setVisibility(View.VISIBLE);
            } else {
                mBtnActionNegative.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 设置错误信息
     * @param text
     */
    public void setErrorInfo(String text) {
        if (text != null) {
            mTvError.setText(text);
        }
    }

    public void setActionNegativeButton(String text) {
        setActionNegativeButton(text, null);
    }

    public void setActionPositiveButton(String text) {
        setActionPositiveButton(text, null);
    }

    /**
     * 设置左侧按钮文本
     * @param text
     */
    public void setActionNegativeButton(String text, OnActionNegativeListener listener) {
        if (text != null) {
            mBtnActionNegative.setVisibility(View.VISIBLE);
            mBtnActionNegative.setText(text);
            if (listener != null) {
                mNegativeListener = listener;
            }
        } else {
            mBtnActionNegative.setVisibility(View.GONE);
        }
    }

    /**
     * 设置右侧按钮文本
     * @param text
     */
    public void setActionPositiveButton(String text, OnActionPositiveListener listener) {
        if (text != null) {
            mBtnActionPositive.setVisibility(View.VISIBLE);
            mBtnActionPositive.setText(text);
            if (listener != null) {
                mPositiveListener = listener;
            }
        } else {
            mBtnActionPositive.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_action_negative:
                if (mNegativeListener != null) {
                    mNegativeListener.onActionNegative();
                }
                break;
            case R.id.btn_action_positive:
                if (mPositiveListener != null) {
                    mPositiveListener.onActionPositive();
                }
                break;
//            case R.id.rl_error:
//                mListener.onAction();
//                break;
        }
//        setStatue(LOADING);
    }

    public void setOnActionNegativeListener(OnActionNegativeListener mListener) {
        this.mNegativeListener = mListener;
    }

    public void setOnActionPositiveListener(OnActionPositiveListener mListener) {
        this.mPositiveListener = mListener;
    }

    public interface OnActionNegativeListener {
        void onActionNegative();
    }

    public interface OnActionPositiveListener {
        void onActionPositive();
    }
}
